package com.example.demo.controller;

import com.example.demo.entity.Book;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


/**
 * Created by think on 2017/7/20.
 */
@RestController
public class HellowController {
    private final Logger logger= Logger.getLogger(getClass());

    @Autowired
    private Book book;

    @Autowired
    private DiscoveryClient client;

    //GET第一种请求方式
    @RequestMapping(value="/hello",method = RequestMethod.GET)
    public String index(){
        ServiceInstance instance = client.getLocalServiceInstance();
        logger.info("/hello,host:"+instance.getHost()+",service_id:"+
                instance.getServiceId());
        return "hellow World My Name is " + book.getAuthor();
    }

    //POST第一种请求方式 postForEntity函数
    @RequestMapping(value="/book",method = RequestMethod.POST)
    public Book index(@RequestBody Book bookN){
        ServiceInstance instance = client.getLocalServiceInstance();
        logger.info("/hello,host:"+instance.getHost()+",service_id:"+
                instance.getServiceId());
        bookN.setName("Zhoufeng");
        return bookN;
    }
}
